<?php
	require_once (dirname(__FILE__)."/../config/Config.inc");
	require_once (dirname(__FILE__)."/../config/api.inc");
	
	$result['status'] = '1';
	
	$method = getParam('m','get','zidian');
	
	$parm = requestArrayParm($_REQUEST);
	
	if($method == 'zidian')
	{
		unset($wheres,$pages,$sorts);
		
		if(!empty($parm['lc_type']))
		{
			
			$wheres['lc_type'] = $parm['lc_type'];
		}
		
		
		$wheres['status'] = '2';
		$sorts['addtime'] = 'desc';
		
		$resData =_SqlTool::getList('pingtai', $wheres, $pages, $sorts);
		foreach ($resData as $key => $value)
		{
			unset($temp);
			$temp['id'] = $value['id'];
			$temp['name'] = $value['pt_name'];
			$list[] = $temp;
		}
		
		$result['list'] = $list;
	}
	
	if($method == 'search')
	{
		unset($wheres,$pages,$sorts);
		
		if(!empty($parm['lc_type']))
		{
			
			$wheres['lc_type'] = $parm['lc_type'];
		}

		if(!empty($parm['keyword']))
		{
			$wheres['pt_name'] = array('like',$parm['keyword']);
		}
		
		if(!empty($parm['lx']))
		{
			$wheres['pt_type'] = $parm['lx'];
		}
		
		if(!empty($parm['bj']))
		{
			$wheres['pt_bj'] = $parm['bj'];
		}
		
		if(!empty($parm['nh']))
		{
			if($parm['nh'] == '1')
			{
				unset($query,$between);
				$query['cp_status'] = '2';
				$query['cp_yqnh'] = array('<=','8');
				$resData = _SqlTool::getDistinctList('chanpin', $query,'pt_id', $between);
			}
			
			if($parm['nh'] == '2')
			{
				unset($query,$between);
				$query['cp_status'] = '2';
				$between['start'] = '8';
				$between['end'] = '12';
				$between['ziduan'] = 'cp_yqnh';
				$resData = _SqlTool::getDistinctList('chanpin', $query,'pt_id', $between);
			}
			
			if($parm['nh'] == '3')
			{
				unset($query,$between);
				$query['cp_status'] = '2';
				$between['start'] = '12';
				$between['end'] = '16';
				$between['ziduan'] = 'cp_yqnh';
				$resData = _SqlTool::getDistinctList('chanpin', $query,'pt_id', $between);
			}
			
			if($parm['nh'] == '4')
			{
				unset($query,$between);
				$query['cp_status'] = '2';
				$query['cp_yqnh'] = array('>=','16');
				$resData = _SqlTool::getDistinctList('chanpin', $query,'pt_id', $between);
			}
			
			unset($pt_ids);
			foreach($resData['list'] as $key => $value)
			{
				$pt_ids[] = $value['pt_id'];
			}
			
			if(empty($pt_ids))
			{
				$result['list'] = array();
				echoJSON($result);
			}

			$wheres['id'] = array('in',join(',',$pt_ids));
		}
		
		if(!empty($parm['qx']))
		{
			if($parm['qx'] == '1')
			{
				unset($query,$between);
				$query['cp_status'] = '2';
				$query['cp_tzqx'] = array('<=','3');
				$query['cp_tzqxlx'] = 'month';
				
				$resData1 = _SqlTool::getDistinctList('chanpin', $query,'pt_id', $between);
			
				unset($query,$between);
				$query['cp_status'] = '2';
				$query['cp_tzqx'] = array('<=','90');
				$query['cp_tzqxlx'] = 'day';
				
				$resData2 = _SqlTool::getDistinctList('chanpin', $query,'pt_id', $between);
			}
			
			if($parm['qx'] == '2')
			{
				unset($query,$between);
				
				$between['start'] = '4';
				$between['end'] = '6';
				$between['ziduan'] = 'cp_tzqx';
				$query['cp_tzqxlx'] = 'month';
				$query['cp_status'] = '2';
				$resData1 = _SqlTool::getDistinctList('chanpin', $query,'pt_id', $between);
				
				unset($query,$between);
				$between['start'] = '91';
				$between['end'] = '180';
				$between['ziduan'] = 'cp_tzqx';
				$query['cp_tzqxlx'] = 'day';
				$query['cp_status'] = '2';
				$resData2 = _SqlTool::getDistinctList('chanpin', $query,'pt_id', $between);
			}
			
			if($parm['qx'] == '3')
			{
				unset($query,$between);
				$between['start'] = '7';
				$between['end'] = '12';
				$between['ziduan'] = 'cp_tzqx';
				$query['cp_tzqxlx'] = 'month';
				$query['cp_status'] = '2';
				$resData1 = _SqlTool::getDistinctList('chanpin', $query,'pt_id', $between);
			
				unset($query,$between);
				$between['start'] = '181';
				$between['end'] = '360';
				$between['ziduan'] = 'cp_tzqx';
				$query['cp_tzqxlx'] = 'day';
				$query['cp_status'] = '2';
				$resData2 = _SqlTool::getDistinctList('chanpin', $query,'pt_id', $between);
			}
			
			if($parm['qx'] == '4')
			{
				unset($query,$between);
				$query['cp_tzqx'] = array('>','12');
				$query['cp_tzqxlx'] = 'month';
				$query['cp_status'] = '2';
				$resData1 = _SqlTool::getDistinctList('chanpin', $query,'pt_id', $between);
				
				unset($query,$between);
				$query['cp_tzqx'] = array('>=','361');
				$query['cp_tzqxlx'] = 'day';
				$query['cp_status'] = '2';
				$resData2 = _SqlTool::getDistinctList('chanpin', $query,'pt_id', $between);
			}
			
			unset($pt_ids);
			foreach($resData1['list'] as $key => $value)
			{
				$pt_ids[] = $value['pt_id'];
			}
			
			foreach($resData2['list'] as $key => $value)
			{
				$pt_ids[] = $value['pt_id'];
			}
			
			if(empty($pt_ids))
			{
				$result['list'] = array();
				echoJSON($result);
			}

			$wheres['id'] = array('in',join(',',$pt_ids));
		}
		
		$wheres['status'] = '2';
		$sorts['addtime'] = 'desc';
		
		$resData =_SqlTool::getList('pingtai', $wheres, $pages, $sorts);
		foreach ($resData as $key => $val)
		{

			unset($temp);
			$temp['id'] = $val['id'];
			$temp['logo'] = OSS_URL.$val['pt_logo'];
			$temp['name'] = $val['pt_name'];
			$temp['ptbj'] = $PT_BJ[$val['pt_bj']];
			$temp['fxdj'] = $val['pt_fxdj'];
			$temp['yhcg'] = $val['pt_yhcg'];
			unset($query,$between);
			$query['pt_id'] = $val['id'];
			$query['cp_status'] = '2';
			$temp['yqnh_max'] = _SqlTool::getMax('chanpin', $query, $between, 'cp_yqnh');
			$temp['yqnh_min'] = _SqlTool::getMin('chanpin', $query, $between, 'cp_yqnh');
			
			$temp['tags'] = array($temp['ptbj'],$temp['yhcg'],$temp['fxdj']);
			
			unset($query);
			$query['pt_id'] = $val['id'];
			$query['cp_status'] = '2';
			$query['cp_type'] = '2';
			$blc = _SqlTool::getCount('chanpin', $query);
			
			if($blc > 0)
			{
				$temp['type'] = '2';
				unset($query);
				$query['pt_id'] = $val['id'];
				$query['cp_status'] = '2';
				$query['cp_type'] = '2';
				$temp['zgfl'] = _SqlTool::getMax('chanpin', $query, $between, 'cp_fl');
			}
			else
			{
				$temp['type'] = '1';
				unset($query);
				$query['pt_id'] = $val['id'];
				$temp['zgfl'] = _SqlTool::getMax('cp_gdfa', $query, $between, 'fanli');
			}
			
			$resData = _SqlTool::getMinByFileds('chanpin', $query, $between, 'cp_tzqx','cp_tzqxlx');
			$temp['qixian'] = $resData['min'];
			$temp['qixianlx'] = $resData['cp_tzqxlx'];
			$temp['qtje'] = _SqlTool::getMin('chanpin', $query, $between, 'cp_qtje');
			
			
			$list[] = $temp;
		}
		
		$result['list'] = $list;
	}

	if($method == 'cpinfo')
	{
		if(empty($parm['id']))
		{
			$result['status'] = '0';
			$result['msg'] = '缺少参数id';
			echoJSON($result);
			exit;
		}
		
		unset($query);
		$query['id'] = $parm['id'];
		$query['cp_status'] = '2';
		$chanpin = _SqlTool::getInfo('chanpin', $query);
		if(empty($chanpin))
		{
			$result['status'] = '0';
			$result['msg'] = '未查询到产品信息';
			echoJSON($result);
			exit;
		}
		
		unset($query);
		$query['id'] = $chanpin['pt_id'];
		$pingtai = _SqlTool::getInfo('pingtai', $query);
		unset($info);
		
		$pingtai['ptbj'] = $PT_BJ[$pingtai['pt_bj']];
		$pingtai['fxdj'] = $pingtai['pt_fxdj'];
		$pingtai['yhcg'] = $pingtai['pt_yhcg'];
		$pingtai['desc'] = $pingtai['pt_desc'];
		unset($query,$between);
		$query['pt_id'] = $pingtai['id'];
		$query['cp_status'] = '2';
		$pingtai['yqnh_max'] = _SqlTool::getMax('chanpin', $query, $between, 'cp_yqnh');
		$pingtai['yqnh_min'] = _SqlTool::getMin('chanpin', $query, $between, 'cp_yqnh');
		$pingtai['tags'] = array($info['ptbj'],$info['yhcg'],$info['fxdj']);
		unset($query);
		$query['pt_id'] = $pingtai['id'];
		$query['cp_status'] = '2';
		$query['cp_type'] = '2';
		$blc = _SqlTool::getCount('chanpin', $query);
		
		if($blc > 0)
		{
			$pingtai['type'] = '2';
			unset($query);
			$query['pt_id'] = $pingtai['id'];
			$query['cp_status'] = '2';
			$query['cp_type'] = '2';
			$pingtai['zgfl'] = _SqlTool::getMax('chanpin', $query, $between, 'cp_fl');
		}
		else
		{
			$pingtai['type'] = '1';
			unset($query);
			$query['pt_id'] = $pingtai['id'];
			$pingtai['zgfl'] = _SqlTool::getMax('cp_gdfa', $query, $between, 'fanli');
		}
		
		$pingtai['sxsj'] = $pingtai['pt_sxsj'];
		$pingtai['szd'] = $pingtai['pt_szd'];
		$pingtai['zyyw'] = $pingtai['pt_zyyw'];
		$pingtai['yhlj'] = $pingtai['pt_yhlj'];
		$pingtai['cjed'] = $pingtai['pt_cjed'];
		$pingtai['txfy'] = $pingtai['pt_txfy'];
		$pingtai['lxglf'] = $pingtai['pt_lxglf'];
		$pingtai['zqzr'] = $pingtai['pt_zqzr'];
		
		
		
		$info = $chanpin;
		$info['logo'] = OSS_URL.$pingtai['pt_logo'];
		$info['name'] = $info['cp_mc'];
		$info['style'] = $CP_FS[$info['cp_style']];
		$info['yqnh'] = $info['cp_yqnh'];
		if($info['cp_type'] == '1')
		{
			unset($query);
			$query['cp_id'] = $info['id'];
			$info['zgfl'] = _SqlTool::getMax('cp_gdfa', $query, $between, 'fanli');
			
			unset($query);
			$query['cp_id'] = $info['id'];
			$info['cp_gdfa'] = _SqlTool::getList('cp_gdfa', $query);
		}
		if($info['cp_type'] == '2')
		{
			$info['zgfl'] = $info['cp_fl'];
		}
		$info['qixian'] = $info['cp_tzqx'];
		$info['qixianlx'] = $info['cp_tzqxlx'];
		$info['qtje'] = $info['cp_qtje'];
		$info['url'] = SITE_URL."skip.php?id=".$info['id'].'&loginkey='.$parm['loginkey'];
		
		$info['cp_flgl'] = nl2br($info['cp_flgl']); 
		
		
		unset($wheres,$pages,$sorts,$between,$list);
		$wheres['pt_id'] = $info['pt_id'];
		$wheres['cp_status'] = '2';
		$wheres['id'] = array('<>',$info['id']);
		$resData = _SqlTool::getList('chanpin', $wheres, $pages, $sorts, $between);
		
		foreach ($resData as $key => $val)
		{
			unset($temp);
			$temp['id'] = $val['id'];
			$temp['logo'] = OSS_URL.$pingtai['pt_logo'];
			$temp['name'] = $val['cp_mc'];
			$temp['style'] = $CP_FS[$val['cp_style']];
			$temp['yqnh'] = $val['cp_yqnh'];
			if($val['cp_type'] == '1')
			{
				unset($query);
				$query['cp_id'] = $val['id'];
				$temp['zgfl'] = _SqlTool::getMax('cp_gdfa', $query, $between, 'fanli');
			}
			if($val['cp_type'] == '2')
			{
				$temp['zgfl'] = $val['cp_fl'];
			}
			$temp['qixian'] = $val['cp_tzqx'];
			$temp['qixianlx'] = $val['cp_tzqxlx'];
			$temp['qtje'] = $val['cp_qtje'];
			$temp['url'] = SITE_URL."skip.php?id=".$val['id'].'&loginkey='.$parm['loginkey'];
			$temp['type'] = $val['cp_type'];
			$temp['cp_lxxs'] = $v['cp_lxxs'];
			$temp['is_new'] = $v['is_new'];
			$temp['lc_type'] = $v['lc_type'];
			$list[] = $temp;
		}
		
		$info['cplist'] = $list;
		
		
		$result['info'] = $info;
		
		$result['ptinfo'] = $pingtai;
	}

	if($method == 'ptinfo')
	{
		if(!empty($parm['id']))
		{
			unset($query);
			$query['id'] = $parm['id'];
			$query['status'] = '2';
			$pingtai = _SqlTool::getInfo('pingtai', $query);
			if(empty($pingtai))
			{
				$result['status'] = '0';
				$result['msg'] = '未查询到平台信息';
				echoJSON($result);
				exit;
			}
			
			unset($info);
			$info['id'] = $pingtai['id'];
			$info['logo'] = OSS_URL.$pingtai['pt_logo'];
			$info['name'] = $pingtai['pt_name'];
			$info['ptbj'] = $PT_BJ[$pingtai['pt_bj']];
			$info['fxdj'] = $pingtai['pt_fxdj'];
			$info['yhcg'] = $pingtai['pt_yhcg'];
			$info['desc'] = $pingtai['pt_desc'];
			unset($query,$between);
			$query['pt_id'] = $pingtai['id'];
			$query['cp_status'] = '2';
			$info['yqnh_max'] = _SqlTool::getMax('chanpin', $query, $between, 'cp_yqnh');
			$info['yqnh_min'] = _SqlTool::getMin('chanpin', $query, $between, 'cp_yqnh');
			$info['tags'] = array($info['ptbj'],$info['yhcg'],$info['fxdj']);
			unset($query);
			$query['pt_id'] = $pingtai['id'];
			$query['cp_status'] = '2';
			$query['cp_type'] = '2';
			$blc = _SqlTool::getCount('chanpin', $query);
			
			if($blc > 0)
			{
				$info['type'] = '2';
				unset($query);
				$query['pt_id'] = $pingtai['id'];
				$query['cp_status'] = '2';
				$query['cp_type'] = '2';
				$info['zgfl'] = _SqlTool::getMax('chanpin', $query, $between, 'cp_fl');
			}
			else
			{
				$info['type'] = '1';
				unset($query);
				$query['pt_id'] = $pingtai['id'];
				$info['zgfl'] = _SqlTool::getMax('cp_gdfa', $query, $between, 'fanli');
			}
			
			$info['sxsj'] = $pingtai['pt_sxsj'];
			$info['szd'] = $pingtai['pt_szd'];
			$info['zyyw'] = $pingtai['pt_zyyw'];
			$info['yhlj'] = $pingtai['pt_yhlj'];
			$info['cjed'] = $pingtai['pt_cjed'];
			$info['txfy'] = $pingtai['pt_txfy'];
			$info['lxglf'] = $pingtai['pt_lxglf'];
			$info['zqzr'] = $pingtai['pt_zqzr'];
			
			unset($wheres,$pages,$sorts,$between,$list);
			$wheres['pt_id'] = $pingtai['id'];
			$wheres['cp_status'] = '2';
			$resData = _SqlTool::getList('chanpin', $wheres, $pages, $sorts, $between);
			
			foreach ($resData as $key => $val)
			{
				unset($temp);
				$temp['id'] = $val['id'];
				$temp['logo'] = OSS_URL.$pingtai['pt_logo'];
				$temp['name'] = $val['cp_mc'];
				$temp['style'] = $CP_FS[$val['cp_style']];
				$temp['yqnh'] = $val['cp_yqnh'];
				if($val['cp_type'] == '1')
				{
					unset($query);
					$query['cp_id'] = $val['id'];
					$temp['zgfl'] = _SqlTool::getMax('cp_gdfa', $query, $between, 'fanli');
				}
				if($val['cp_type'] == '2')
				{
					$temp['zgfl'] = $val['cp_fl'];
				}
				$temp['qixian'] = $val['cp_tzqx'];
				$temp['qixianlx'] = $val['cp_tzqxlx'];
				$temp['qtje'] = $val['cp_qtje'];
				$temp['url'] = SITE_URL."skip.php?id=".$val['id'].'&loginkey='.$parm['loginkey'];
				$temp['type'] = $val['cp_type'];
				$temp['cp_lxxs'] = $v['cp_lxxs'];
				$temp['is_new'] = $v['is_new'];
				$list[] = $temp;
			}
			
			$info['cplist'] = $list;
			
			$result['info'] = $info;
		}
		else
		{
			$result['status'] = '0';
			$result['msg'] = '缺少参数id';
			echoJSON($result);
			exit;
		}
	}
	
	echoJSON($result);